if (SERVER) then
	AddCSLuaFile("cl_init.lua")
	AddCSLuaFile("shared.lua")
end

if (CLIENT) then	
	SWEP.Slot				= 3
	SWEP.SlotPos			= 13
	SWEP.IconLetter			= ","
	SWEP.ViewModelFlip		= false

	killicon.AddFont("shot_energy", "HL2MPTypeDeath", SWEP.IconLetter, Color(100, 100, 100, 255))
end

SWEP.PrintName			= "Plasma Gun"	
SWEP.Base				= "weapon_cs_base"
SWEP.HoldType			= "ar2"
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.ViewModel			= "models/weapons/v_irifle.mdl"
SWEP.WorldModel			= "models/weapons/w_irifle.mdl"

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false

SWEP.Primary.Sound			= Sound("Weapon_AR2.Double")
SWEP.Primary.Recoil			= 0.00000000001
SWEP.Primary.Damage			= 2
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.0001
SWEP.Primary.ClipSize		= -1
SWEP.Primary.Delay			= 0.25
SWEP.Primary.DefaultClip	= 0
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"
SWEP.Primary.Battery		= 100
SWEP.Primary.Chargerate		= .25
SWEP.Primary.Mode		= 1

SWEP.Secondary.Sound			= Sound("Weapon_AR2.Double")
SWEP.Secondary.Recoil			= 1
SWEP.Secondary.Damage			= 40
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Cone			= 0.0001
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.Delay			= 1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic		= false
SWEP.Secondary.Ammo			= "none"


SWEP.IronSightsPos 		= Vector(-2.7, -8, 2.225)
SWEP.IronSightsAng 		= Vector(0, -4, 0)

function SWEP:Reload()
	self.Weapon:DefaultReload(ACT_VM_RELOAD)
	self:SetIronsights(false)
	self.Owner:SetFOV(90,.3)
end

function SWEP:Think()
	if self.LastCharge<CurTime()-self.Primary.Chargerate && self.Primary.Battery<100 then
		self.Primary.Battery=self.Primary.Battery+2
		if self.Primary.Battery>100 then self.Primary.Battery=100 end
		self.LastCharge = CurTime()
	end
end

function SWEP:PrimaryAttack()

	local delay = self.Primary.Delay
	self.Weapon:SetNextSecondaryFire(CurTime() + delay)
	self.Weapon:SetNextPrimaryFire(CurTime() + delay)
	
	self.LastCharge = CurTime()+delay*1.5
	if (self.Primary.Battery<5) then 
		return 
	end
	self.Primary.Battery=self.Primary.Battery-5
	self.Weapon:EmitSound(self.Primary.Sound)
	if SERVER then
		self.Weapon:FireShot()
	end

	self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	
end

function SWEP:Initialize()

	self:SetWeaponHoldType(self.HoldType)
	
	self.Weapon:SetNetworkedBool("Ironsights", false)
	self.LastCharge = CurTime()
	self.Halfcharge = false
end

function SWEP:FireShot()

	local object = ents.Create("shot_energy")
	if ValidEntity(object) then
		object:SetOwner(self.Owner)
		object:SetPos(self.Owner:GetShootPos()+self.Owner:EyeAngles():Right()*5)
		object:SetAngles(self.Owner:EyeAngles())
		object:SetVelocity(self.Owner:GetAimVector()*1000)
		object:Spawn()
		if SERVER then
			if self:GetUpgrade() > 0 == true then
				object.Upgraded = true
			end
			object:SetColor(tonumber(self.Owner:GetInfo("rpdm_clcolor_r")),tonumber(self.Owner:GetInfo("rpdm_clcolor_g")),tonumber(self.Owner:GetInfo("rpdm_clcolor_b")),255)
		end
	end
	
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) 		// View model animation
	self.Owner:MuzzleFlash()								// Crappy muzzle light
	self.Owner:SetAnimation(PLAYER_ATTACK1)				// 3rd Person Animation

end

function SWEP:GetViewModelPosition(pos, ang)
	local Mul = 1.0
	local Offset	= self.IronSightsPos
	if (self.IronSightsAng) then
	
		ang = ang * 1
		ang:RotateAroundAxis(ang:Right(), 		self.IronSightsAng.x * Mul)
		ang:RotateAroundAxis(ang:Up(), 		self.IronSightsAng.y * Mul)
		ang:RotateAroundAxis(ang:Forward(), 	self.IronSightsAng.z * Mul)
	
	
	end
	
	local Right 	= ang:Right()
	local Up 		= ang:Up()
	local Forward 	= ang:Forward()
	
	

	pos = pos + Offset.x * Right * Mul
	pos = pos + Offset.y * Forward * Mul
	pos = pos + Offset.z * Up * Mul

	return pos, ang
end

function SWEP:SecondaryAttack()
end

function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)	
	draw.SimpleText(self.IconLetter, "HL2MPTypeDeath", x + wide/2, (y + tall*0.2)-10, Color(255, 210, 0, 255), TEXT_ALIGN_CENTER)
	if (self:GetUpgrade() > 0) then
		draw.SimpleText("K", "CSKillIcons", x + wide-20, y + tall-25, Color(200,200, 200, 255), TEXT_ALIGN_CENTER)
	end
end

function SWEP:Upgrade(bool)
	self.Upgradestring = "Larger battery"
end